package com.lyz.weixinservice;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration // 配置类
public class WechatMpConfig {

    // 从配置文件中读取 appid
    @Value("${wechat.mp.appid}")
    private String appid;

    // 从配置文件中读取 secret
    @Value("${wechat.mp.secret}")
    private String secret;

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        // 设置配置（包含 appid 和 secret）
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    // 配置存储
    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        config.setAppId(appid);   // 设置 appid
        config.setSecret(secret); // 设置 secret
        return config;
    }
}